import moment from "moment";

export default function getBeginDateFromToday(count) {
    let beginDate = moment(new Date());
    const updataDate = moment(sessionStorage.getItem("updateDate"));
    let currDate = beginDate;
    const dates = sessionStorage
        .getItem("holidays")
        .split(",")
        .map((m) => ({
            date: m.split("|")[0],
            remark: m.split("|")[1],
        }));

    if (updataDate && updataDate.format("YYYY-MM-DD") == beginDate.format("YYYY-MM-DD")) {
        count--;
    }



    while (count > 0) {
        beginDate = beginDate.subtract(1, "days");

        const week = currDate.day();
        if (week == 6 || week == 0) {
            continue;
        }

        let find = dates.find((f) => f.date == currDate.format("YYYY-MM-DD"));
        if (find) {
            continue;
        }

        count--;


    }

    return beginDate;
}